package com.apestech.framework.file;

import java.util.Date;

import com.apestech.framework.file.callback.FileKeyCallBackHandler;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.pinyin.Converter;
import com.apestech.framework.pinyin.PinyinConverter;
import com.apestech.framework.util.StringUtil;
import com.apestech.framework.util.DateUtil;

public class KeyStrategy extends StringUtil{
	
	public enum KeySaveType {
		Initials(0),  //首字母 
		date(1);      //日期
	      
	    int value;  
	      
	    KeySaveType(int value) {  
	        this.value = value;  
	    }  
	      
	    public int getValue() {  
	        return value;  
	    }  
	}
	
	private String prefix="";   //前缀
	private String suffix="";   //后缀
	private KeySaveType keySaveType=KeySaveType.Initials;
	private String[] keylist;
	private String keyField=null;
	private int keyLength=1;
	private Converter pinyin= new PinyinConverter();
	private String pinyinField=null;
	
	public KeyStrategy(){
		
	}
	
	public KeyStrategy(KeySaveType keySaveType){
		this.keySaveType=keySaveType;
	}
	
	public KeyStrategy(KeySaveType keySaveType, String prefix, String suffix){
		this.keySaveType=keySaveType;
		this.prefix=prefix;
		this.suffix=suffix;
	}
	
	public void setKeyField(String keyField){
		if(keyField!=null){
			if(!rightStr(keyField, 1).equals(";")){
				keyField=keyField+";";
			}
			this.keylist=keyField.split(";");
		} else {
			this.keylist=null;
		}
		this.keyField=keyField;
	}
	
	public String getKeyField(){
		return this.keyField;
	}
	
	public void setPinyinField(String pinyinField){
		this.pinyinField=pinyinField;
	}
	
	public String getPinyinField(){
		return this.pinyinField;
	}
	
	public void setPrefix(String prefix){
		this.prefix=prefix;
	}
	
	public void setSuffix(String suffix){
		this.suffix=suffix;
	}
	
	public void setKeyLength(int keyLength){
		this.keyLength=keyLength;
	}
	
	public int getKeyLength(){
		return this.keyLength;
	}
	
	public String getKey(String text, boolean bvalue) throws Exception{
		String key="";
		text=getKeyValue(text, bvalue);
		if(this.keySaveType==KeySaveType.Initials){
			if(this.keyLength>0){
				key=leftStr(text, this.keyLength);
			} else {
				key=text;
			}
		} else if(this.keySaveType==KeySaveType.date){
			key=DateUtil.dateToStr(new Date());
		}
		return this.prefix+key+this.suffix;
	}
	
	public String getKey(String text) throws Exception{
		return getKey(text, false);
	}
	
	public String getKey(String text, FileKeyCallBackHandler fileKeyCallBackHandler, boolean bvalue) throws Exception{
		String keytext=getKeyValue(text, bvalue);
		return this.prefix+fileKeyCallBackHandler.getKey(text, this.keyField, keytext)+this.suffix;
	}
	
	public String getKey(String text, FileKeyCallBackHandler fileKeyCallBackHandler) throws Exception{
		return getKey(text, fileKeyCallBackHandler, false);
	}
	
	public String getKeyValue(String text, boolean bvalue) throws Exception{
		StringBuilder sb=new StringBuilder();
		int len=this.keylist.length;
		for(int i=0; i<len; i++){
			String key=this.keylist[i];
			if(key!=null){
				String value=text;
				if(!bvalue){
					value=getNKeyValue(key, text);
				}
				if(this.pinyinField!=null){
					if((";"+this.pinyinField+";").indexOf(";"+key+";")>=0){
						value=pinyin.getPinyinAbbreviation(value);
						value=value.replace(" ", "");
					}
				}
				sb.append(value);
			}
		}
		return sb.toString().trim();
	}
	
	public String getKeyValue(String text) throws Exception{
		return getKeyValue(text, false);
	}
	
	public String getRowKeyValue(String keyField){
		StringBuilder sb=new StringBuilder();
		int len=this.keylist.length;
		for(int i=0; i<len; i++){
			String key=this.keylist[i];
			if(key!=null){
				String value=getNKeyValue(key, keyField);
				sb.append(value);
			}
		}
		return sb.toString().trim();
	}
	
	private String getNKeyValue(String keyField, String text){
		if(keyField==null) return "undefined";
		int i1=text.indexOf(keyField);
		i1=i1+keyField.length()+3;
		int i2=text.indexOf("\"",i1+1);
		String key=text.substring(i1, i2);
		return key;
	}
	
	

}
